package kyle.test;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class CameraConnection {

	public class CameraServerURL {
		public static final String IP = "192.168.1.1";
		public static final int PORT = 8080;
		public static final String FIRMWARE_VERSION = "http://" + IP
				+ "/cgi-bin/getver";
		public static final String UPDATE_FIRMWARE = "http://" + IP
				+ "/cgi-bin/update";
		public static final String REBOOT_DEVICE = "http://" + IP
				+ "/cgi-bin/reboot";
		public static final String SET_TIME = "http://" + IP
				+ "/cgi-bin/settime";
		public static final String GET_FILELIST = "http://" + IP
				+ "/cgi-bin/playlist";
		public static final String SEND_GPS = "http://" + IP
				+ "/cgi-bin/setgps";
		public static final String RESET_RTSP = "http://" + IP
				+ "/cgi-bin/media?restart=1";
		public static final String SETAP = "http://"+IP+"/cgi-bin/setap";
	}

	public static final int CODE_FIRMWARE_VERSION = 101;
	public static final int CODE_UPDATE_FIRMWARE = 102;
	public static final int CODE_REBOOT_DEVICE = 103;
	public static final int CODE_SET_TIME = 104;
	public static final int CODE_GET_FILELIST = 105;
	public static final int CODE_SEND_GPS = 106;
	public static final int CODE_RESET_RTSP = 107;

	private final String TAG = "CameraConnection";
	private final boolean DEBUG = true;
	private String mResponse = null;
	private static Object mLock = new Object();
	private ResponseHandler mRH = null;

	// private Context mContext = null;
	// private Handler mHandler = new Handler() {
	// public void handleMessage(Message msg) {
	// if (mRH != null) {
	// mRH.handleResponse(msg.arg1, (String) msg.obj);
	// }
	// }
	// };

	public CameraConnection(ResponseHandler responseHandler) {
		mRH = responseHandler;
	}

	// public boolean isCameraAvailable() {
	// ConnectivityManager mConnectivity = (ConnectivityManager) mContext
	// .getSystemService(Context.CONNECTIVITY_SERVICE);
	// NetworkInfo info = mConnectivity
	// .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	// if (info.isConnected()) {
	// Log.d(TAG, "Wifi is connected");
	// return mConnectivity.requestRouteToHost(
	// ConnectivityManager.TYPE_WIFI, ipToInt(CameraServerURL.IP));
	// } else {
	// return false;
	// }
	// }

	private int ipToInt(String addr) {
		String[] addrArray = addr.split("\\.");
		int num = 0;
		for (int i = 0; i < addrArray.length; i++) {
			int power = 3 - i;
			num += ((Integer.parseInt(addrArray[i]) % 256 * Math
					.pow(256, power)));
		}
		return num;
	}

	public void getFirmwareVersion() {
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest
							.doGet(CameraServerURL.FIRMWARE_VERSION);
					sendMessage(CODE_FIRMWARE_VERSION, mResponse);
				}
			}
		}.start();
	}

	public void updateFirmware(String filename, String type, String version,
			String md5) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair pair1 = new BasicNameValuePair("file", filename);
		NameValuePair pair2 = new BasicNameValuePair("type", type);
		NameValuePair pair3 = new BasicNameValuePair("ver", version);
		NameValuePair pair4 = new BasicNameValuePair("md5", md5);

		params.add(pair1);
		params.add(pair2);
		params.add(pair3);
		params.add(pair4);

		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doPost(
							CameraServerURL.UPDATE_FIRMWARE, params);
					sendMessage(CODE_UPDATE_FIRMWARE, mResponse);
				}
			}
		}.start();
	}

	public void rebootDevice() {
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest
							.doGet(CameraServerURL.REBOOT_DEVICE);
					sendMessage(CODE_REBOOT_DEVICE, mResponse);
				}
			}
		}.start();
	}

	public void setTime(String time) {
		time.toString();
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair pair1 = new BasicNameValuePair("date",
				time);//.format("YYYYMMDDHHMM.SS")
		params.add(pair1);
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doPost(CameraServerURL.SET_TIME,
							params);
					sendMessage(CODE_SET_TIME, mResponse);
				}
			}
		}.start();
	}

	public void getFilelist() {
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doGet(CameraServerURL.GET_FILELIST);
					sendMessage(CODE_GET_FILELIST, mResponse);
				}
			}
		}.start();
	}

	public void sendGPS(float latitude, float longtitude) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair pair1 = new BasicNameValuePair("x",
				String.valueOf(latitude));
		NameValuePair pair2 = new BasicNameValuePair("y",
				String.valueOf(longtitude));
		params.add(pair1);
		params.add(pair2);
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doPost(CameraServerURL.SEND_GPS,
							params);
					sendMessage(CODE_SEND_GPS, mResponse);
				}
			}
		}.start();
	}

	public void resetRTSP() {
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doGet(CameraServerURL.RESET_RTSP);
					sendMessage(CODE_RESET_RTSP, mResponse);
				}
			}
		}.start();
	}
	public void setSSID(String ssid){
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair pair1 = new BasicNameValuePair("ssid",
				ssid);//.format("YYYYMMDDHHMM.SS")
		params.add(pair1);
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doPost(CameraServerURL.SETAP,
							params);
					sendMessage(CODE_SET_TIME, mResponse);
				}
			}
		}.start();
	}
	public void setPassword(String password){
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair pair1 = new BasicNameValuePair("passwd",
				password);//.format("YYYYMMDDHHMM.SS")
		params.add(pair1);
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doPost(CameraServerURL.SETAP,
							params);
					sendMessage(CODE_SET_TIME, mResponse);
				}
			}
		}.start();
	}
	public void restartAP(){
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair pair1 = new BasicNameValuePair("restart",
				"1");//.format("YYYYMMDDHHMM.SS")
		params.add(pair1);
		new Thread() {
			public void run() {
				synchronized (mLock) {
					mResponse = HttpRequest.doPost(CameraServerURL.SETAP,
							params);
					sendMessage(CODE_SET_TIME, mResponse);
				}
			}
		}.start();
	}
	
	public static String _FUNC_() {
		StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
		return traceElement.getMethodName() + "\n";
	}

	private void sendMessage(int code, String response) {
		// if (DEBUG)
		// Log.d(TAG, "code is " + code + " response is " + mResponse);
		// Message msg = new Message();
		// msg.arg1 = CODE_FIRMWARE_VERSION;
		// msg.obj = new String(mResponse);
		// mHandler.sendMessage(msg);
		System.out.print(code + " " + "response is " + response);
	}

	public interface ResponseHandler {
		public void handleResponse(int request_code, String response);
	}
}
